Page ({
  data:{
    item:0,
    tab:0,
    playlists:[{
      id : 1,title:'如歌',singer:'张杰',src:'/music/如歌 - 张杰.mp3',coverImgUrl:'/images/推荐.png'},
      {id : 2,title:'感谢你曾经来过',singer:'阿涵',src:'/music/感谢你曾来过 - Ayo97 _ 阿涵.mp3',coverImgUrl:'/images/推荐.png'},
      {id : 3,title:'来自天堂的魔鬼',singer:'邓紫棋',src:'/music/来自天堂的魔鬼 - G.E.M. 邓紫棋.mp3',coverImgUrl:'/images/推荐.png'},
      {id : 4,title:'桃花妆',singer:'鸾音社',src:'/music/桃花妆 - 鸾音社.mp3',coverImgUrl:'/images/推荐.png'}
    ],
    state:'paused',
    playIndex:0,
    play:{
      currentTime:'00:00',
      duration:'00:00',
      percent:0,
      title:'',
      singer:'',
      coverImgUrl:'/images/推荐.png'
    },
  },
  changeItem:function(e){
    this.setData({
      item: e.target.dataset.item
    })
  },
  changeTab:function(e){
    this.setData({
      tab:e.detail.current
    })
  },
  audioCtx:null,
  onReady:function(){
    this.audioCtx = wx.createInnerAudioContext()
    //默认选择第一曲
    this.setMusic(0)
  },
  setMusic:function(index){
    var music = this.data.playlists[index]
    this.audioCtx.src = music.src
    this.setData({
      playIndex:index,
      'play.title':music.title,
      'play.singer':music.singer,
      'play.coverImgUel':music.coverImgUrl,
      'play.currentTime':'00:00',
      'play.duration':'00:00',
      'play.percent':0
    })
  },
  play:function(){
    this.audioCtx.play()
    this.setData({state:'renning'})
  },
  pause:function(){
    this.audioCtx.pause()
    this.setData({state:'paused'})
  },
  next:function() {
    var index = this.data.playIndex >= this.data.playlists.length - 1 ?
                0 : this.data.playIndex + 1
    this.setMusic(index)
    if (this.data.state === 'running'){
      this.play()
    }
  }
})
